﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// ListSelectView.xaml 的交互逻辑
    /// </summary>
    public partial class ListSelectView : UserControl
    {
        CourseWrapper course;
        MainWindowManager manager;
        int listCount = 10;

        ObservableCollection<ListData> listCollection =
        new ObservableCollection<ListData>();

        public ObservableCollection<ListData> ListCollection
        {
            get { return listCollection; }
        }

        public string Header
        {
            get { return course.CourseName + " " + listCount + " Lists"; }
        }

        public ListSelectView(CourseWrapper course, MainWindowManager manager)
        {
            this.course = course;
            this.manager = manager;
            listCount = ListRepository.GetListCount(course);

            var allList = ListRepository.GetAllListData(course);
            foreach (var data in allList)
            {
                listCollection.Add(new ListData
                {
                    ListName = data.ListName,
                    ListCount = data.ListCount,
                    ListFirst = data.ListFirst,
                    ListId = data.ListId
                });
            }

            InitializeComponent();
        }

        private double CalcProf(List<WordWrapper> list)
        {
            double result = 0;
            int n = 0;
            foreach (WordWrapper ww in list)
            {
                WordInfo wi = ww.WordInfo;
                if (wi.TotalMemCount > 0)
                {
                    n += wi.TotalMemCount;
                    result += wi.TotalProficiency * wi.TotalMemCount;
                }
            }
            return n > 0 ? result / n : 0;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            ListData data = (ListData)this.listView1.SelectedItem;
            
            ListShowView showView = new ListShowView(course, data.ListId);
            var mainWindow = MainWindowManager._mainWindow;
            mainWindow.grid1.Children.Clear();
            mainWindow.grid1.Children.Add(showView);
        }

    }

    public class ListData
    {
        public string ListName { get; set; }
        public string ListCount { get; set; }
        public string ListProf { get; set; }
        public string ListFirst { get; set; }
        public int ListId { get; set; }
    }
}
